﻿// This visitor class defines the content of the info window that appears when
// clicking on a marker. During the visit, we format only the markers that have 
// the _mustBeFormatted flag set to true.
MapControl2.AAAMarkersFormatterVisitor = function(){

    this._mapDiv = null;
    this._mapDivId = null;
    this._map = null;
    this._virtual_map = null;
};

MapControl2.AAAMarkersFormatterVisitor.prototype = {
    
    // ---------------------- Virtual Earth bird view integration ----------------------------------
    _populateBirdseyeTab : function(tab, gmarker, routes)
    {
        var g_map_closure  = this._map;
        var marker_closure = gmarker;
        
        if (this._virtual_map == null) 
        {
            this._virtual_map = new MapControl2.AAAVirtualMap(g_map_closure);
        }
        
        var v_map_closure = this._virtual_map;
        
        tab.content = "<div id='embedded_map' style=\"position:relative; width:250px; height:250px;\"></div>";
        
        tab.initialize = function()
        {
           v_map_closure.load("embedded_map", marker_closure._geoloc);
        }
        
        tab.destruct = function()
        {
            v_map_closure.removeFromDiv();
        } 
    },
    // --------------------------------------------------------------------------------------------
    
    // ---------------- formatting section --------------------------------------------------------
    visit: function(element, element_type)
    {
        switch(element_type)
        {   
            case MapControl2.AAATRIP_ELEMENT:   // stages marker formatting
            
                if (this._mapDiv == null)
                {
                    this._map = element.getParentMap();
                    this._mapDiv = this._map.getParentDiv();
                    this._mapDivId = this._mapDiv.id;
                }
            
                var markers = element.getMarkers();
                var routes = element.getRoutes();
                var i = 0;
                var currentRoute;
                
                // tab indexes
                var routeInfoTabIndex = 0;
                var tripInfoTabIndex = 0;
                var birdViewTabIndex = 1; 
                var oneFormatted = false;
                
                for (i = 0; i<markers.length; i++)
                {
                    if (markers[i]._mustBeFormatted)
                    {
                        oneFormatted = true;
                        markers[i]._mustBeFormatted = false;
                        var infoTab = new Object(); 
                        var mTabs = markers[i].getTabs(); 
                        var birdViewTab = new Object();
                    
                        if (i < markers.length - 1)
                        {
                            currentRoute = routes[i].getRouteModel();
                        
                            // constructs the info
                            var routeDescription = '<b>' + mapStrings['start_info_route']+ '</b>: ' + currentRoute.start.address + '<br />';
                            if (currentRoute.start.description !== null)
                            {
                                routeDescription += '<b>' + mapStrings["startDescr_info_route"]+ '</b>: ' + 
                                                currentRoute.start.description + '<br />';
                            }
                            routeDescription += '<b>'+mapStrings["end_info_route"]+'</b>: ' + currentRoute.end.address + '<br />';
                            if (currentRoute.end.description !== null)
                            {
                                routeDescription += '<b>'+mapStrings["endDescr_info_route"]+'</b>: ' + currentRoute.end.description + '<br />';
                            }
                            routeDescription += '<b>'+mapStrings["dist_info_route"]+'</b>: ' + currentRoute.distance + 'km<br />';
                            routeDescription += '<b>'+mapStrings["time_info_route"]+'</b>: ' + currentRoute.time + ' min.<br />';
                            routeDescription += '<b>'+mapStrings["wayNumb_info_route"]+'</b>: ' + currentRoute.viaPoints.length;
                            
                            var users = currentRoute.start.gettingInUsers.length;
                            if (users > 0)
                                routeDescription += '<br /><b>' + mapStrings['passengers_in'] + '</b>: ' + users;
                            users = currentRoute.start.gettingOffUsers.length;
                            if (users > 0)
                                routeDescription += '<br /><b>' + mapStrings['passengers_off'] + '</b>: ' + users;
                            
                            infoTab.label = mapStrings["title_info_route"];
                            infoTab.content = routeDescription;
                            
                            if (checkBrowserSupport())
	                        {
	                            birdViewTab.label = 'Bird View';
                                this._populateBirdseyeTab(birdViewTab, markers[i], routes);
                            }
                        }else{
                            // trattamento del marker di fine viaggio
                            currentRoute = routes[routes.length - 1].getRouteModel();
                            infoTab.label = mapStrings["endLabel_trip"];
                        
                            var endDetails = '<b>'+mapStrings["end_trip"]+'</b>: ' + currentRoute.end.address + '<br />';
                            if (currentRoute.end.description !== null)
                                endDetails += '<b>'+mapStrings["endDescr_trip"]+'</b>: ' + currentRoute.end.description;
                            
                            if(currentRoute.end.gettingInUsers == null)
                                currentRoute.end.gettingInUsers = new Array();
                                
                            var users = currentRoute.end.gettingInUsers.length;
                            if (users > 0)
                                endDetails += '<br /><b>' + mapStrings['passengers_in'] + '</b>: ' + users;
                           
                            if(currentRoute.end.gettingOffUsers == null)    
                                currentRoute.end.gettingOffUsers = new Array();
                           
                            users = currentRoute.end.gettingOffUsers.length;
                            if (users > 0)
                                endDetails += '<br /><b>' + mapStrings['passengers_off'] + '</b>: ' + users;
                            
                            infoTab.content = endDetails;
                            
                            if (checkBrowserSupport())
	                        {
	                            birdViewTab.label = 'Bird View';
                                this._populateBirdseyeTab(birdViewTab, markers[i], routes);
                            }
                        }
                    
                        
                        // when the informations have been created, they are stored in the 
                        // _tabs Field of the associated AAAMarker object.
                        if(i == 0)
                        {
                            mTabs[routeInfoTabIndex + 1] = infoTab;
                            if (checkBrowserSupport()) mTabs[birdViewTabIndex + 1] = birdViewTab;
                        }
                        else
                        {
                            mTabs[routeInfoTabIndex] = infoTab;
                            if (checkBrowserSupport()) mTabs[birdViewTabIndex] = birdViewTab;
                        }
                    }
                }
                
                if (oneFormatted)
                {
                    // in the first marker of the trip we add another tab that
                    // contains all the trip info.
                    var trip = element.getTripModel();
                    var tripInfo = '<b>'+mapStrings["start_info_trip"]+'</b>: ' + trip.routes[0].start.address + '<br />';
                    if (trip.routes[0].start.description !== null)
                    {
                        tripInfo += '<b>'+mapStrings["startDescr_info_trip"]+'</b>: ' + trip.routes[0].start.description + '<br />';
                    }
                    tripInfo += '<b>'+mapStrings["end_info_trip"]+'</b>: ' + trip.routes[trip.routes.length - 1].end.address + '<br />';
                    if (trip.routes[trip.routes.length - 1].end.description !== null)
                    {
                        tripInfo += '<b>'+mapStrings["endDescr_info_trip"]+'</b>: ' + trip.routes[trip.routes.length - 1].end.description + '<br/>';
                    }
                    tripInfo += '<b>'+mapStrings["dist_info_trip"]+'</b>: ' + trip.CoveredDistance + 'km<br />';
                    tripInfo += '<b>'+mapStrings["time_info_trip"]+'</b>: ' + trip.SpentTime + 'min.<br />';
                    tripInfo += '<b>'+mapStrings["routeN_info_trip"]+'</b>: ' + trip.routes.length;
                
                    var tripDetailsTab = new Object();
                    tripDetailsTab.label = mapStrings["title_info_trip"];
                    tripDetailsTab.content = tripInfo;
                    var startTabs = markers[0].getTabs();
                    startTabs[0] = tripDetailsTab;
                
                    // now we format the waypoints info
                    for(i = 0; i<routes.length; i++)
                    {
                        routes[i].accept(this);
                    }
                }
            break;
            
            case MapControl2.AAAROUTE_ELEMENT: // stage marker info format
                
                var waypoints = element.getWaypoints();
                if (waypoints.length > 0)
                {
                    var routeModel = element.getRouteModel();
                    var i = 0;
                
                    for (i = 0; i<waypoints.length; i++)
                    {
                        if (waypoints[i]._mustBeFormatted)
                        {
                            waypoints[i]._mustBeFormatted = false;
                            var waypointInfoTab = new Object();
                            waypointInfoTab.label =mapStrings["WaypointLabel"];
                        
                            var waypointDetails = '<b>'+mapStrings["waypoint_Location"]+'</b>: ' + routeModel.viaPoints[i].address + '<br />';        
                            if (routeModel.viaPoints[i].description !== null)
                            {
                                waypointDetails += '<b>'+mapStrings["waypoint_Descr"]+'</b>: ' + routeModel.viaPoints[i].description;
                            }
                        
                            waypointInfoTab.content = waypointDetails;
                            
                            if (checkBrowserSupport())
	                        {
                                var birdViewTab = new Object();
                                birdViewTab.label = 'Bird View';
                                this._populateBirdseyeTab(birdViewTab, waypoints[i], new Array(element));
                                waypoints[i]._tabs[1] = birdViewTab;
                            }
                            waypoints[i]._tabs[0] = waypointInfoTab;
                            
                        }
                    }
                }
            break;
        }
    },
    
    getResult: function()
    {
        return null;
    }
    // -----------------------------------------------------------------------------------------------
};

MapControl2.AAAMarkersFormatterVisitor.registerClass
    ('MapControl2.AAAMarkersFormatterVisitor');
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
